package com.rpc.center.discover.client.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class PipleLineInitializer extends ChannelInitializer<SocketChannel> {
    private NettyDiscoverServiceImpl nettyDiscoverService;
    private NettyDiscoverServiceHandler handler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ch.pipeline().addLast(new ObjectDecoder(1024*1024,
                ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
        ch.pipeline().addLast(new ObjectEncoder());
        ch.pipeline().addLast(handler);
    }

    public PipleLineInitializer(NettyDiscoverServiceImpl nettyDiscoverService) {
        this.nettyDiscoverService = nettyDiscoverService;
        handler=new NettyDiscoverServiceHandler(nettyDiscoverService);
    }
}
